package rechit;

public class RecHit {
   
    // Trajectory equation as before, Returns X coord when supplied with Y.
    
    private static double trajectoryX(double y,double deg,double v, double g) {
        
        // Deg -> Rad
        double phi = deg * 0.0174532925;
        
        // A,B,C values
        
        double A = ((-g)/(v * v * Math.pow(Math.cos(phi),2)));
        double B = Math.tan(phi);
        double C = y;
        
        // Two outputs for + and -
        
        double x1 = ((-B + Math.sqrt(B*B - (4*A*C)))/ (2*A));
        double x2 = ((-B - Math.sqrt(B*B - (4*A*C)))/ (2*A));
        
        // In this case one will always be 0 so find the one which is
        // and return the other.
        
        if(x1 == 0.0) return x2;
        else return x1;
    }
    
private static double trajectoryY(double x,double deg,double v, double g) {
        
        // Deg -> Rad
        double phi = deg * 0.0174532925;
        
        // output: Theres no quadratic equation this time.
        
        double output = x * Math.tan(phi) - ((g * x * x) / (v* v * Math.cos(phi)  * Math.cos(phi)));
        
        // return the result
        return output;
    }
    
    // Function that checks hits given a velocity,angle, and target Rectangle
    
    public static boolean checkHits(double v,double phi,Rectangle rec) {
        // Closest x value
        double x1 = rec.topLeftCornerX();
        // Furthest x Value
        double x2 = rec.topRightCornerX();
        // Set output to false as default
        boolean output = false;
        
        // For every x value in the rectangle from x1 to x2
        // check whether the (x,y) value hits the rectangle
        // if so change output to true, give the x,y values
        // and break the loop
        // if not, then do nothing
        for(double i=x1;i<=x2;i += 0.1) {
            double hit = trajectoryY(i,v,phi,9.81);
            if(rec.checkHit(i,hit)== true) {
                output = true;
                System.out.print("x: " + i + "\ny: " + hit + "\n");
                break;
            }
        }
        
        return output;
        
    }
    
    // Main. Takes the input velocity and Degrees
    
    public static void main(String[] args) {
        // Create a rectangle with a top left point of x:20, y:10
        // it is 5m long and 10m high. It therefore occupies
        // x: 20-25m and y:0-10m
        Rectangle target = new Rectangle(20d,10d,5d,10d);
        double v = Double.parseDouble(args[0]);
        double phi = Double.parseDouble(args[1]);
        // hit is the where the missile lands if y=0
        double hit = trajectoryX(0d,v,phi,9.81d);
        
        // if it hits the rectangle then print Hit
        if(checkHits(v,phi,target)==true) {
            System.out.println("Hit.");
        }
        
        // else Print where the missile lands.
        else {
            System.out.println("You missed. The missile landed at " + hit + "m.");
        }
    }
        
}
